package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bean.Admin;
import com.dao.AdminDao;
import com.dao.BaseDao;

public class AdminDaoImpl implements AdminDao {

	@Override
	public Admin seleAdminByName(Admin admin) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from admin where adminName = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, admin.getAdminName());
			rs = ps.executeQuery();
			if(rs.next()) {
				admin.setAdminId(rs.getInt("adminId"));
				admin.setAdminName(rs.getString("adminName"));
			}else {
				admin = new Admin();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return admin;
	}

	@Override
	public Admin seleAdmin(Admin admin) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from admin where adminName = ? and password = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1, admin.getAdminName());
			ps.setString(2, admin.getPassword());
			rs = ps.executeQuery();
			if(rs.next()) {
				admin.setAdminId(rs.getInt("adminId"));
				admin.setAdminName(rs.getString("adminName"));
				admin.setPassword(rs.getString("password"));
			}else {
				admin = new Admin();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return admin;
	}

}
